#!/usr/bin/python
# -*- coding: utf-8 -*-


#twitter-utils version 0.1
#Programed by Takuji Tokiwa (tokiwatch@gmail.com).
#Published under New BSD License.

import os
import ConfigParser
import twitter
from optparse import OptionParser

###########################################################################################################

#Import Config file and Setup

CONFIG_FILE = os.getenv('HOME') + os.sep + '.twitrc'

conf = ConfigParser.SafeConfigParser()
conf.read(CONFIG_FILE)

USERNAME = conf.get('twitter_account', 'username')
PASSWORD = conf.get('twitter_account', 'password')
ENCODE = conf.get('terminal_setup','encode') 

###########################################################################################################

#set up command line options

usage = "usage: %prog [options]"
version="%prog 0.1"
parser = OptionParser(usage=usage, version=version)
parser.add_option("-u", "--user", action="store", type="string", dest="screen_name", default=USERNAME,
                  help="User Name")
parser.add_option("-d", "--dump", action="store_true", dest="dump", 
                  help="Dump mode")
parser.add_option("--friends", action="store_true", dest="friends", 
                  help="Show User's Friends")
parser.add_option("--followers", action="store_true", dest="followers", 
                  help="Show User's Followers (This option works with your account only.)")

#analyse options
#imported options are stored as "options" instance

(options, args) = parser.parse_args()

###########################################################################################################

#Fetch Twitter

api = twitter.Api(USERNAME, PASSWORD)

user  = api.GetUser(options.screen_name)
user_info = [str(user.id), 
			user.name,
			user.screen_name,
			user.location,
			user.description,
			user.url,
			user.status.text]
table_name = ["ID",
			"User Name",
			"Screen Name",
			"location",
			"description",
			"url",
			"status"]

if options.friends:
	user_friends = api.GetFriends(options.screen_name)

if options.followers:
	user_followers = api.GetFollowers()

if options.dump:
	for i in range(len(user_info)):
		print table_name[i].encode(ENCODE)+ ':' + user_info[i].encode(ENCODE)
	if options.friends:
		print 'friends:',
		for j in range(len(user_friends)):
			print user_friends[j].screen_name.encode(ENCODE) + ',' ,
		print ""
	if options.followers:
		print 'followers:',
		for k in range(len(user_followers)):
			print user_followers[k].screen_name.encode(ENCODE) + ',' ,
		print ""

else:
	for i in range(len(user_info)):
		print '\t' + table_name[i].ljust(11).encode(ENCODE) + '\t' + user_info[i].encode(ENCODE)
	if options.friends:
		for j in range(len(user_friends)):
			print '\t' + 'friends'.ljust(11) + '\t' + user_friends[j].screen_name
	if options.followers:
		for k in range(len(user_followers)):
			print '\t' + 'followers'.ljust(11) + '\t' + user_followers[k].screen_name


